package com.xxgc.qinglian.qinglianserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxgc.qinglian.qinglianserver.entity.TimingEntity;
import com.xxgc.qinglian.qinglianserver.entity.TimingEntityRepository;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/Timing")
@Tag(name = "计时器接口")
public class TimingController {

    @Autowired
    private TimingEntityRepository timingEntityRepository;

//    存储计时时长
    @PostMapping("/saveTiming")
    public boolean saveTiming(@RequestBody Map<String, Object> map) {
        TimingEntity timingEntity = new TimingEntity();
        Object projectIdObj = map.get("projectId");
        if (projectIdObj == null || projectIdObj.toString().trim().isEmpty()) {
            throw new IllegalArgumentException("projectId cannot be null or empty");
        }
        timingEntity.setProjectId(Integer.parseInt(projectIdObj.toString()));
        Object totalTimeObj = map.get("totalTime");
        if (totalTimeObj == null || totalTimeObj.toString().trim().isEmpty()) {
            throw new IllegalArgumentException("totalTime cannot be null or empty");
        }
        timingEntity.setTotalTime(Integer.parseInt(totalTimeObj.toString()));
        return timingEntityRepository.save(timingEntity);
    }

//    查询对应projectId的时长
    @PostMapping("/getTimingByProjectId")
    public TimingEntity getTimingByProjectId(@RequestBody Map<String, Object> map) {
        QueryWrapper<TimingEntity> qw = new QueryWrapper<>();
        qw.eq("project_id",Integer.parseInt(map.get("projectId").toString()));
        return timingEntityRepository.getOne(qw);
    }
}
